选择框上选择某个选项后,清除文本框

时间:2015-10-17 03:06:48

标签: html forms

我有一个选择框和文本框,如下所示:

<?php
    $receipt="12345";
?>

<form>
    <select name="sent" id="sent">            
        <option value="1">Yes</option>
        <option value="0">No</option>
    </select> 

    <input type="text" name="receipt" value="<?=$receipt?>">
</form>

选择后,如何清除文本框?

3 个答案:

答案 0 :(得分:0)

您可以使用jQuery change method

来实现它

创建变量receipt

var receipt = "12345"; //replace 12345 with <?php echo $receipt;?>

使用jQuery更改方法$("#sent").change(function ()

绑定select元素

选中针对var receipt的select元素值,如果为true,则清除输入。

&#13;
&#13;
$(document).ready(function () {
	var receipt = "123456";
	$("#sent").change(function () {
		var sel = $(this).val();
		if (sel == 0) {
			$("input[name=receipt]").val("");
		} else {
			$("input[name=receipt]").val(receipt);
		}
	});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <select name="sent" id="sent">
        <option value="1">Yes</option>
        <option value="0">No</option>
    </select>
    <input type="text" name="receipt" value="123456">
</form>
&#13;
&#13;
&#13;

Fiddle

答案 1 :(得分:0)

试试这个 这是工作 我试过了

<html>
<head>

<script>
function Clear()
{
    var sent = document.getElementById('sent').value;
    if(sent==0){
        document.getElementById("receipt").value="";
    }


}
</script>

</head>

<body>
    <?php
    $receipt="12345";
?>

<form>
    <select name="sent" id="sent" onchange="Clear()">            
        <option value="1">Yes</option>
        <option value="0">No</option>
    </select> 

    <input type="text" id="receipt" name="receipt" value="<?=$receipt?>">

</form>
</body>
</html>

答案 2 :(得分:0)

<?php
    $receipt="12345";
?>

<script>
function SetField(val)
{
    if(val==0)
        document.getElementById('input-field').value='';
    else
        document.getElementById('input-field').value='<?php echo $receipt;?>';
}
</script>
<form>
    <select name="sent" id="sent" onchange="SetField(this.value);">            
        <option value="1">Yes</option>
        <option value="0">No</option>
    </select> 

    <input type="text" id="input-field" name="receipt" value="<?=$receipt?>">
</form>

非常紧凑