c# - Change textbox text in JavaScript - Stack Overflow

I'm allowing a user to use either one of two textboxes to search a database - one is an ID field,

I'm allowing a user to use either one of two textboxes to search a database - one is an ID field, and one is a freetext field. I'm using ASP.NET with C# btw.

Anyway, all I need to do is have it so when a user clicks on one of the text boxes, the other text box text is simply deleted, so the other text box is blank.

How would I do this? I'm guessing JavaScript is the solution.

I'm allowing a user to use either one of two textboxes to search a database - one is an ID field, and one is a freetext field. I'm using ASP.NET with C# btw.

Anyway, all I need to do is have it so when a user clicks on one of the text boxes, the other text box text is simply deleted, so the other text box is blank.

How would I do this? I'm guessing JavaScript is the solution.

Share Improve this question edited Oct 7, 2010 at 16:39 Chase Florell 47.5k59 gold badges190 silver badges382 bronze badges asked Oct 7, 2010 at 16:20 ChrisChris 7,62122 gold badges105 silver badges196 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2

Given a function in javascript:

function clearOther(which){
 document.getElementById(which).value='';
}

this can then be called when you focus on one textbox, passing the id of the other:

<input type="text" id="box1" onfocus="clearOther('box2')" />
<input type="text" id="box2" onfocus="clearOther('box1')"  />

working example --> http://jsfiddle/CwWKn/

[Demo]

var tbox1 = document.getElementById('tbox1');
var tbox2 = document.getElementById('tbox2');

tbox1.onfocus = function() {
  tbox2.value = "";
};

tbox2.onfocus = function() {
  tbox1.value = "";
};

I +1'ed Jamiec 's answer, but you still have to have some logic for those who don't have JS enabled. Maybe use the ID field as a priority if BOTH field's are populated.

if ((!box1.IsNullOrEmpty) & (!box2.IsNullOrEmpty)) {
    // Evaluate box1 (since box1 is the ID field)
} else {
    if ((!box1.IsNullOrEmpty)) {
        // Evaluate box1
    } else {
        // Evaluate box2
    }
}
//Page A
<input type='text' id='tb'>
 var returnedValue = showModalDialog('page2.aspx', window);

//Page B
<input type='text' onkeypress='update(this);'>

function update(Sender) {
var input = window.dialogArguments.document.getElementById("tb");
input.value = Sender.value
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745137643a4613278.html

相关推荐

  • c# - Change textbox text in JavaScript - Stack Overflow

    I'm allowing a user to use either one of two textboxes to search a database - one is an ID field,

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信