Ben*_*Ben 253 jquery hidden field onchange event-handling
我有一个隐藏的文本字段,其值通过AJAX响应更新.
<input type="hidden" value="" name="userid" id="useid" />
Run Code Online (Sandbox Code Playgroud)
当这个值改变时,我想发出一个AJAX请求.任何人都可以建议如何检测变化?
我有以下代码,但不知道如何查找值:
$('#userid').change( function() {
alert('Change!');
})
Run Code Online (Sandbox Code Playgroud)
yyc*_*man 599
所以这是迟到的,但我发现了一个答案,以防它对遇到这个帖子的人有用.
隐藏元素的值更改不会自动触发.change()事件.因此,无论您在何处设置该值,您还必须告诉jQuery触发它.
function setUserID(myValue) {
$('#userid').val(myValue)
.trigger('change');
}
Run Code Online (Sandbox Code Playgroud)
一旦发生这种情况,
$('#userid').change(function(){
//fire your ajax call
})
Run Code Online (Sandbox Code Playgroud)
应该按预期工作.
lul*_*ala 27
由于隐藏输入不会在更改时触发"更改"事件,因此我使用MutationObserver来触发此操作.
(有时隐藏的输入值更改由您无法修改的其他脚本完成)
这在IE10及以下版本中不起作用
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var trackChange = function(element) {
var observer = new MutationObserver(function(mutations, observer) {
if(mutations[0].attributeName == "value") {
$(element).trigger("change");
}
});
observer.observe(element, {
attributes: true
});
}
// Just pass an element to the function to start tracking
trackChange( $("input[name=foo]")[0] );
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用以下功能,您也可以更改类型元素.
$("input[type=hidden]").bind("change", function() {
alert($(this).val());
});
Run Code Online (Sandbox Code Playgroud)
隐藏元素的值更改不会自动触发.change()事件.因此,无论您在何处设置该值,您还必须告诉jQuery触发它.
HTML
<div id="message"></div>
<input type="hidden" id="testChange" value="0" />
Run Code Online (Sandbox Code Playgroud)
JAVASCRIPT
var $message = $('#message');
var $testChange = $('#testChange');
var i = 1;
function updateChange() {
$message.html($message.html() + '<p>Changed to ' + $testChange.val() + '</p>');
}
$testChange.on('change', updateChange);
setInterval(function() {
$testChange.val(++i).trigger('change');;
console.log("value changed" +$testChange.val());
}, 3000);
updateChange();
Run Code Online (Sandbox Code Playgroud)
应该按预期工作.
小智 6
$('#userid').change(function(){
//fire your ajax call
});
$('#userid').val(10).change();
Run Code Online (Sandbox Code Playgroud)
基于Viktar 的回答,这里有一个实现,您可以在给定的隐藏输入元素上调用一次,以确保每当输入元素的值发生变化时都会触发后续的更改事件:
/**
* Modifies the provided hidden input so value changes to trigger events.
*
* After this method is called, any changes to the 'value' property of the
* specified input will trigger a 'change' event, just like would happen
* if the input was a text field.
*
* As explained in the following SO post, hidden inputs don't normally
* trigger on-change events because the 'blur' event is responsible for
* triggering a change event, and hidden inputs aren't focusable by virtue
* of being hidden elements:
* /sf/answers/1238686781/
*
* @param {HTMLInputElement} inputElement
* The DOM element for the hidden input element.
*/
function setupHiddenInputChangeListener(inputElement) {
const propertyName = 'value';
const {get: originalGetter, set: originalSetter} =
findPropertyDescriptor(inputElement, propertyName);
// We wrap this in a function factory to bind the getter and setter values
// so later callbacks refer to the correct object, in case we use this
// method on more than one hidden input element.
const newPropertyDescriptor = ((_originalGetter, _originalSetter) => {
return {
set: function(value) {
const currentValue = originalGetter.call(inputElement);
// Delegate the call to the original property setter
_originalSetter.call(inputElement, value);
// Only fire change if the value actually changed.
if (currentValue !== value) {
inputElement.dispatchEvent(new Event('change'));
}
},
get: function() {
// Delegate the call to the original property getter
return _originalGetter.call(inputElement);
}
}
})(originalGetter, originalSetter);
Object.defineProperty(inputElement, propertyName, newPropertyDescriptor);
};
/**
* Search the inheritance tree of an object for a property descriptor.
*
* The property descriptor defined nearest in the inheritance hierarchy to
* the class of the given object is returned first.
*
* Credit for this approach:
* /sf/answers/2716182171/
*
* @param {Object} object
* @param {String} propertyName
* The name of the property for which a descriptor is desired.
*
* @returns {PropertyDescriptor, null}
*/
function findPropertyDescriptor(object, propertyName) {
if (object === null) {
return null;
}
if (object.hasOwnProperty(propertyName)) {
return Object.getOwnPropertyDescriptor(object, propertyName);
}
else {
const parentClass = Object.getPrototypeOf(object);
return findPropertyDescriptor(parentClass, propertyName);
}
}
Run Code Online (Sandbox Code Playgroud)
在文档准备好时调用它,如下所示:
$(document).ready(function() {
setupHiddenInputChangeListener($('myinput')[0]);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
250181 次 |
| 最近记录: |