我在react-widgets文档中读到了:
name
type: string
The HTML name attribute, passed directly to the input element.
我在Combobox onChange函数中获取name属性值是什么。所以我写道:
<Combobox
data={featuredWeightList}
valueField="id"
textField="displayName"
name="featuredWeight"
defaultValue={featuredWeight}
value={featuredWeight}
onChange={this.handleFeaturedWeightChange}
/>
handleFeaturedWeightChange = (evt) => {
const id = evt.id;
const id = evt.displayName;
// I do not have name property in evt variable
};
我在evt变量中没有name属性,只有“id”和“displayName”属性。
但是正常的html输入,我可以直接在onChange函数中获取name属性。像这样:
<Input
type="text"
name="address"
value={door.address}
onChange={this.handleInputChange}
/>
handleInputChange = (evt) => {
const target = evt.target;
const name = target.name;
console.log('Input value: ', value);
console.log('Input name: ', name);
};
有人对如何在onChange函数中获取Combobox名称属性值有一些建议吗?
谢谢!
答案 0 :(得分:1)
可以使用javascript bind()函数。
function a(name, evt) {
console.log('name', name);
console.log('evt', evt);
};
var b = a.bind(this, 'featuredWeight');
b({a: 'a', b: 'b'});
所以看起来应该是这样的:
<Combobox
data={featuredWeightList}
valueField="id"
textField="displayName"
name="featuredWeight"
defaultValue={featuredWeight}
value={featuredWeight}
onChange={this.handleFeaturedWeightChange.bind(this, "featuredWeight")}
/>
handleFeaturedWeightChange = (name, evt) => {
const id = evt.id;
const id = evt.displayName;
// I do not have name property in evt variable
console.log('name', name);
};
另一种方法(没有bind()):
function a(name) {
return function(evt) {
console.log('name', name)
}
};
var b = a('featuredWeight');
b({a: 'a', b: 'b'});
答案 1 :(得分:1)
我以这种方式解决了这个问题(希望它可以帮助其他人):
handleComboboxChange = (data, param) => {
console.log(data, param);
//Output: {id: 5, displayName: 5} "featuredWeight"
};
<Combobox
data={featuredWeightList}
valueField="id"
textField="displayName"
name="featuredWeight"
defaultValue={featuredWeight}
value={featuredWeight}
onChange={(param) => this.handleComboboxChange(param, 'featuredWeight')}
/>
答案 2 :(得分:0)
您可以使用此变量来读取当前选定的值。 在onchange函数中写下面的代码行。
$('#RequestTypeId').change(function (e) {
if ($(this).val() === "1") {
//your code
}