gpt4 book ai didi

javascript - 在 jquery.ajax 之后更改 html 元素

转载 作者:行者123 更新时间:2023-11-28 06:22:52 26 4
gpt4 key购买 nike

在通过 post 方法发送代码后,我尝试更改 HTML 元素#result

我尝试使用 .html()/.replace() 但它们不起作用。

这是我的代码

JS:

$("form input[type=submit]").click(function() {
var your_selected_value = $('#bkb').val();
$.ajax({
type: "POST",
url: 'getpro.php',
data: {
"selected": your_selected_value
},

success: function(data) {
$('#result').html(data);


},
error: function(data) {

}
});
});

HTML:

<div id="lnw2">
<form method="post" action='#'>
<select id="bkb" name="selectpro">
<?php loadcountry(); ?>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
</div>
<div id="result">
<h3>test</h3>
</div>

PHP:

<? php
if (isset($_POST['selected'])) {
$selected_val = $_POST['selected']; // Storing Selected Value In Variable
echo "answer " .$selected_val; // Displaying Selected Value
}
?>

最佳答案

问题是,您一开始就没有阻止 form 提交,而是使用 preventDefault() 方法。

引用资料如下:

所以你的 jQuery 应该是这样的:

$("form input[type=submit]").click(function(e) {
e.preventDefault();
var your_selected_value = $('#bkb').val();
$.ajax({
type: "POST",
url: 'getpro.php',
data: {
"selected": your_selected_value
},

success: function(data) {
$('#result').html(data);


},
error: function(data) {

}
});
});

关于javascript - 在 jquery.ajax 之后更改 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com