当前位置: 动力学知识库 > 问答 > 编程问答 >

internet explorer - Specific jQuery Code Not Working in IE

问题描述:

I am testing a webpage I have developed, and I have several jQuery scripts running in this webpage.

The webpage displays and functions as expected in Chrome and Firefox, but I am currently testing this in Internet Explorer (version 9).

All the other jQuery scripts are working on this page, except the following script:

$(".SpecificTextboxClass").change(function() {

if ($(this).val() == 3) {

$(".SpecificTextboxClass").not(this).val(0);}});

UPDATE EDIT:

Here is the code for the textbox:

<input type="text" name="scale" class="Validation SpecificTextboxClass" value="1">

Could the double class be causing the problem?

SECOND UPDATE:

I just loaded the webpage in Internet Explorer 6 and had the same problem. I am also using jQuery 1.9.1 (as fas as I know).

THIRD UPDATE:

The problem seems to resolve itself when the validation code is removed:

$('.Validation').keyup(function () {

if ("" != this.value.replace(/([0-9]|10)(\.\d{1,3})?]/g, '')) {

this.value = this.value.replace(/[^0-9\.]/g, '').substring(0,5);}});

网友答案:

Okay, thanks to your jsFiddle, I think I've found the problem.

The bug is in your validation code, in the regex pattern you're using to test whether it's valid -- you have a stray ] character at the end of the pattern.

if ("" != this.value.replace(/([0-9]|10)(\.\d{1,3})?]/g, '')) {
                                                    ^
                                              this is invalid

This is making the test fail every time, which in turn is causing it to change the value in code, which in turn appears to be stopping the change event from firing.

You can fix the problem by removing the rogue ] character.

I'd also suggest using regex .match() rather than .replace() for the test -- replace is the wrong function for an if() test like that.

You could replace it directly with this:

if (!this.value.match(/^(([0-9]|10)(\.\d{1,3})?)*$/)) {

That will have exactly the same functionality but will be a lot more efficient to run.

See here for an update to your jsFiddle to demonstrate the fix. (note that it has console.log() calls in it, so make sure you have F12 dev tools open when you run it in IE)

However even then, I'm not convinced it's excatly what you want. This will (as will your previous version) return valid for strings like 3.1346.21710.45 -- ie multiple values concatenated together. In your replace version, this is caused by the g modifier (which replaces multiple occurrences). In my version, it's done by the * at the end of the patten.

If you only want a single value, remove the g from your version, or replace the * with a ? in mine.

Finally, since it looks like you're just testing for a decimal value, you might also consider just using parseFloat() instead of all that regex.

Hope that helps.

网友答案:

Check your "SpecificTextboxClass" input type, may be you have not added.

网友答案:

I noticed you didn't set up your code to run on document ready/window load. You could be running into a race condition where your code runs before the DOM has had a chance to setup.

Please confirm that you are setting up your code like this:

$( document ).ready(function() {

    $(".TAB1val1").change(function() {
        if ($(this).val() == 3) {
            $(".TAB1val1").not(this).val(0);
        }
    });
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: