Change textbox text in JavaScript

前端 未结 4 1786
离开以前
离开以前 2021-01-26 07:44

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, a

相关标签:
4条回答
  • 2021-01-26 08:12

    [Demo]

    var tbox1 = document.getElementById('tbox1');
    var tbox2 = document.getElementById('tbox2');
    
    tbox1.onfocus = function() {
      tbox2.value = "";
    };
    
    tbox2.onfocus = function() {
      tbox1.value = "";
    };
    
    0 讨论(0)
  • 2021-01-26 08:15

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-26 08:16
    //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
    }
    
    0 讨论(0)
  • 2021-01-26 08:18

    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.net/CwWKn/

    0 讨论(0)
提交回复
热议问题