当我在 val() 函数中使用变量而不是固定值时,没有输出。我该如何修复这个错误。
示例:在下面的示例中,当我使用变量 ($data="Glenn Quagmire")
时,我没有得到任何输出。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
问候
最佳答案
就像这样使用:
$("input:text").val("<?php echo $data; ?>");
完整代码
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>
关于javascript - 在 Jquery.val() 函数中使用变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28041366/