我在我的應用程式中有一個表,它被動態地填充。
我的表格中的一個TD被加載,并且根據值的不同,它可以是一個下拉、文本或日期。
在我的Jquery代碼中,我試圖確定輸入型別是什么,并能夠正確設定值。
function Test(valCd) {
var tr = $('tr[valCd=' valCd ' ]')。
var refVal = tr.find('td[id=refValue] input')。
//After here's the way I'm getting the type (but it only supports types that are not selects)
var type = refVal.attr('type')。
if (type =='text' || type =='number')
...do something
else if (type == 'date')
...do something
//However, I don't see a way that I can identify a `select` since `type = undefined`。
//我想過做類似的事情,但這似乎不是一個好方法。
else if (type === undefined) {
var select = refVal.find('select')?
}
}
上面的Sudo代碼--原諒任何錯誤。
是否有一個好的方法,讓我能夠一次性地確定型別是什么,無論是文本、數字、日期還是選擇?或者有比檢查type====undefined
提前感謝
uj5u.com熱心網友回復:input選擇器只匹配<input>元素,而不是<select>。你可以使用jQuery擴展:input,它匹配所有的用戶控制元件。
然后你可以使用nodeName屬性來告訴它是一個input還是select。
function Test(valCd) {
var tr = $('tr[valCd=' valCd ' ]')。
var refVal = tr.find('td[id=refValue] :input')。
//After here's the way I'm getting the type (but it only supports types that are not selects)
var type = refVal.prop('nodeName') == 'select' ? 'select' : refVal.attr('type')。
if (type =='text' || type =='number')
//...做一些事情。
else if (type == 'date')
//...做一些事情。
else if (type == 'select') {
//...做一些事情。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331831.html
標籤:
