gpt4 book ai didi

javascript - 根据第 2 页中的文本框值禁用第 1 页中的按钮(AJAX、PHP、jQuery)

转载 作者:行者123 更新时间:2023-11-29 12:56:50 25 4
gpt4 key购买 nike

我正在开发一个注册表单。对于用户名字段上的“用户名检查”功能,我使用 AJAX 函数。

我的注册表单:page1.php

<form id="user-form" method="post">
<input id="txtusername" name="txtusername" type="text"onblur="getResult2(this.value);">
<div id="showusername" class="help-block"></div>
<button type="submit" id="butnid" class="btn btn-success" onclick="check()" >Save User</button>
</form>

<script>
function check(){
if(getElementById('showusername').innerHTML=="Username available"){
document.getElementById("user-form").submit();
}
}
</script>

然后我使用 AJAX 函数将文本字段中输入的用户名传递到 page2.php,在其中完成用户名检查。

我的AJAX功能:

// to check whether a user name already exist or not
function getResult2(uname)
{

if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();

}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()//callback fn
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{

document.getElementById("showusername").innerHTML=xmlhttp.responseText;

}
}

xmlhttp.open("GET","page2.php?variable="+uname,true);
// alert(xmlhttp);
xmlhttp.send();

}

这工作正常并将值传递给 page2.php。我想当 page2 中的变量值为 0 时禁用 page1 中的按钮。

我的 page2 代码。

<?php
$un=$_GET["variable"];

//php code takes username from database & runs a for loop to check
//suppose $userName[$i] contains all usernames from database
// x is the number of elements in $username[] array.

for($i=0; $i<x;$i++)
{

if( $un == $userName[$i])
{
echo 'User name already exist';
$ret_var=0;
echo '<input type = "hidden" name = "subject1" id="subject1" value="' . $ret_var . '" />';
?>
<script>
// I want to disable the button in page1 if value of $ret_var is 0.

$(function(){
var txt = $('#subject1').val();
if(txt == 0){
$('#butnid').prop('disabled', true);
}
});

</script>
<?php
exit();
}

}
echo 'User name available';

?>

但是该按钮不会被禁用。有什么帮助或建议吗???

最佳答案

在您的代码中,您使用 value 获取值。它不在 jquery 中。相反,你必须像这样使用

$('#subject1').val();

关于javascript - 根据第 2 页中的文本框值禁用第 1 页中的按钮(AJAX、PHP、jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932654/

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