Can't set a value in a web form using "value='foo'", but entering the value manually is OK?
P粉863295057
P粉863295057 2023-09-13 15:56:10
0
1
412

I tried using a similar method to set the values of several fields in the web form

document.getElementsByName('form[493202]')[0].value="Foo"

If I do this in the browser console, the field will be updated, but if I then manually click on the field, it will revert to the old value. The old values were entered manually by me.

This is a weird thing - if I enter a value manually, everything works as expected, including submitting the form, but if I enter the value using JS and then submit the form, I get a complaint that the field is empty (a Kind of a "compromise" between these results if I first enter the values manually and then change the values using JS. If I then click "Submit" the values revert to what I entered manually.), although from Visually, the fields are not empty - but when I click Submit, the fields are cleared (manually entered values remain after clicking Submit).

How is this going? How do I enter a value that this form "accepts" using JS?

Some HTML in the form, "Title", and this specific field:

P粉863295057
P粉863295057

reply all (1)
P粉436688931

The problem is that when you change the value using JavaScript, the library used to validate that the form is empty does not update.

You may need to fire theinputevent (and maybe thechangeevent) to let these validation libraries know that you have filled out the form.

function myFunction() { var field = document.getElementsByName('form[493202]')[0]; field.value = "Foo" // Trigger the oninput event var inputEvent = new Event('input'); field.dispatchEvent(inputEvent); // Trigger the onchange event var changeEvent = new Event('change'); field.dispatchEvent(changeEvent); }
 
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!