2016-09-23 55 views
1

我需要显示一个基于用户输入的对话框,并且我正在实现Zebra对话框插件来帮助解决这个问题。Javascript变量,设置为HTML对象,总是返回空

当用户点击一个按钮时,我可以得到一个通用的对话框,但无论我做什么,我都无法使用Javascript来查看HTML文本框,更不用说其中的数据了。

我为此创建了一个测试页面。见下文。

下面是HTML/PHP代码(的index.php):

<head> 
    <!-- Style for Zebra Dialog boxes --> 
    <link rel="stylesheet" href="zebra/zebra_dialog.css" type="text/css"> 
</head> 

<header> 
    <h1>Testing My Dialogs and Alerts</h1> 
</header> 

<body> 

    <?php 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $myTyping = trim($_POST["myTyping"]); 

     // Display what the user typed in a dialog. Is there some code that needs to go here? 
    } 
    ?> 

    <form id="form_to_submit" action="index.php" method="POST"> 

     <fieldset> 
      <label>Type anything you want:</label> 
      <input type="text" name="myTyping" id="myTyping"> 
      <button type="submit" id="click">Click Here to show alert and see what you typed.</button> 
     </fieldset> 

    </form> 

<!-- Link to jQuery file so any plug-ins can work 
Including the production version here. Can also download one for better debugging. --> 
<script type="text/javascript" src="jquery-1.12.0.min.js"></script> 

<!-- Link to Zebra Dialog box code --> 
<script type="text/javascript" src="zebra_dialog.js"></script> 

<!-- Link to My Javascript code --> 
<script type="text/javascript" src="myTestScripts.js"></script> 

</body> 

这里是Javascript代码(myTestScripts.js)。我已经尝试了3种不同的方式来获取用户的输入并显示它,但“getElementById”从不起作用。它还没有呈现?我试着把防止默认代码放进去,但这没有什么区别。

/* javascripts */ 

// FIRST TRY 
$(document).ready(function() { 

// show a dialog box when clicking on a button 
$("#click").bind('click', function(e) { 
    //e.preventDefault(); 
    $.Zebra_Dialog('The link was clicked!'); 

    **var myInputElement = document.getElementById("myTyping"), // This doesn't get the element, always is null** 
     myInput = myInputElement.innerText; 

    console.log("myInputElement: " + myInputElement);  
    console.log("myInput: " + myInput); 

    $.Zebra_Dialog('Here is what you typed:', myInput); 
    }); 

}); 

// SECOND TRY 
$('#form_to_submit').submit(function(e) { 
console.log("inside form_to_submit"); 

**var myInputElement = document.getElementById("myTyping"), // also returns null** 
    myInput = myInputElement.innerText; 

    console.log("myInputElement: " + myInputElement);  
    console.log("myInput: " + myInput); 

$.Zebra_Dialog('Here is what you typed:', myInput); 
console.log("leaving form_to_submit"); 

}); 


// THIRD TRY 
window.onsubmit = function (e) { 

console.log("inside window.onsubmit, preventing default"); 
//e.preventDefault(); 

**var myInputElement = document.getElementById("myTyping"), // also returns null** 
    myInput = myInputElement.innerText; 

    console.log("myInputElement: " + myInputElement);  
    console.log("myInput: " + myInput); 

$.Zebra_Dialog('Here is what you typed:', myInput); 
console.log("leaving window.onsubmit"); 
} 

回答

1

你的元素是一个输入,所以innerText不起作用。取而代之的

var myInputElement = document.getElementById("myTyping"),  
    myInput = myInputElement.innerText; 

尝试

var myInputElement = document.getElementById("myTyping"),  
     myInput = myInputElement.value; 

或者干脆

var myInput = document.getElementById("myTyping").value; 

看一看输入文本对象的属性here

+0

当然!谢谢你的发现。 –

1

使用jQuery:

$(document).ready(function() { 
 

 
// show a dialog box when clicking on a button 
 
    $("#click").bind('click', function(e) { 
 
    e.preventDefault(); 
 
    $.Zebra_Dialog('Here is what you typed: '+$("#myTyping").val()); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<head> 
 
    <!-- Style for Zebra Dialog boxes --> 
 
    <link rel="stylesheet" href="http://dynabitlab.it/extensions/demo_virtuemart/modules/mod_vavmm/admin/zebra/css/flat/zebra_dialog.css"  type="text/css"> 
 
    
 
</head> 
 

 
<header> 
 
    <h1>Testing My Dialogs and Alerts</h1> 
 
</header> 
 

 
<body> 
 

 
    <?php 
 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
 
     $myTyping = trim($_POST["myTyping"]); 
 

 
     // Display what the user typed in a dialog. Is there some code that needs to go here? 
 
    } 
 
    ?> 
 

 
    <form id="form_to_submit" action="index.php" method="POST"> 
 

 
     <fieldset> 
 
      <label>Type anything you want:</label> 
 
      <input type="text" name="myTyping" id="myTyping"> 
 
      <button type="submit" id="click">Click Here to show alert and see what you typed.</button> 
 
     </fieldset> 
 

 
    </form> 
 

 
<!-- Link to jQuery file so any plug-ins can work 
 
Including the production version here. Can also download one for better debugging. --> 
 
<script type="text/javascript" src="jquery-1.12.0.min.js"></script> 
 

 
<!-- Link to Zebra Dialog box code --> 
 
<script type="text/javascript" src="zebra_dialog.js"></script> 
 

 
<!-- Link to My Javascript code --> 
 
<script type="text/javascript" src="http://dynabitlab.it/extensions/demo_virtuemart/modules/mod_vavmm/admin/zebra/javascript/zebra_dialog.js"></script> 
 

 
</body>

+0

谢谢你的洞察力! –