2013-05-01 52 views
0

我有一個小表格,只有收音機可以檢查或只有一個輸入文本可以填寫。有6種選擇,但只有價值將被張貼。清除未集中的輸入與jquery

我有這個小jQuery的是取消選中當文本的重點是廣播正相反

$('#angleform input[type=text]').focus(function() { 
    $('input[type=radio]').prop('checked', false); 
}); 

$('#angleform input[type=radio]').click(function() { 
    $('input[type=text]').val(''); 
}); 

我創造了這個小提琴證明 http://jsfiddle.net/Pdh6R/4/

我要的是,如果一個值已經排滿並且我關注另一個輸入,應該清除未集中輸入中的值。

我知道我可以通過輸入ID檢查它,但有沒有一種方法?

+1

難道你不是'$('#angleform input [type =「text」]').val('');'當'text'輸入獲得焦點時? [小提琴](http://jsfiddle.net/Pdh6R/7/) – billyonecan 2013-05-01 07:58:23

+0

@billyonecan - Garh!那很簡單!你會發布答案嗎?謝謝。 – 2013-05-01 08:34:33

回答

2

你可以簡單地清除所有的text輸入時一個獲得焦點:

$('#angleform input[type="text"]').val(''); 
0

兩種方法將清除以前的輸入狀態,無論你的電臺名稱或ID簡單地通過檢查輸入的類型屬性。這樣他們都只使用一個事件處理程序來清除彼此,而無需清除無線電數值!

方法之一:.focus()

var inputs = $("#angleform fieldset input"); 

// bind focus on all inputs 
inputs.focus(function() { 
    // loop through all 
    for (var i = 0; i < inputs.length; i += 1) { 
     var el = inputs.eq(i); 

     // ignore focused element 
     if (inputs[i] !== this) { 
      // if radio type is the same => remove state 
      // all other inputs can have empty value 
      if (el.prop("type") === "radio") { 
       el.prop("checked", false); 
      } else { 
       el.val(""); 
      } 
     } 
    } 
}); 

演示:http://jsfiddle.net/tive/uM3tk/

方法二:.blur()

var inputs = $("#angleform input"), 
    myChoice; 

// bind .blur() on inputs 
inputs.blur(function() { 
    myChoice = $(this); 

    // if input type is radio 
    if (myChoice.prop("type") === "radio") { 
     myChoice.prop("checked", false); 
    } else { 
     myChoice.val(""); 
    } 
}); 

演示:http://jsfiddle.net/tive/LUP4b/