0

我继承了一些不太工作的代码......

我有一个布局对话框的jsp:

<div class="ShippingPoints">
    <div id="dialog-form" title="Shipping Points">
        <p class="validateTips">
            Please include all vendor ship points by product group. If vendor
            ships all products from one location input City, State, Zip Code
            then select "All" for product group.
        </p>

        <fieldset>
            <label font-family="Courier New" align="left" for="city">City</label>
            <input maxlength=50 align="right" type="text" name="city" id="city"
                class="text ui-corner-all" />
            <br />
            <label font-family="Courier New" align="left" for="state">State</label>
            <select maxlength=6 align="right" name="state" id="state"
                class="text ui-corner-all">
                <c:forEach items="${states}" var="state">
                    <option value="${state.fieldValue}">
                        ${state.fieldDescription}
                    </option>
                </c:forEach>
            </select>
            <br />
            <label font-family="Courier New" align="left" for="stateOther">State (Other):</label>
            <input maxlength=6 align="right" type="text" name="stateOther" id="stateOther" value=""
                class="text ui-corner-all" />
            <br />
            <label font-family="Courier New" align="left" for="zip">Zip</label>
            <input align="right" maxlength=10 align="right" type="text" name="zip" id="zip" value=""
                class="text ui-corner-all" />
            <br />
            <label font-family="Courier New" align="left" align="left" for="product">Product</label>
            <input align="right" maxlength=50 type="text" name="product" id="product" value=""
                class="text ui-corner-all" />
            <br />
        </fieldset>
    </div>

在页面上,我有一个打开对话框的链接......和一个调用删除的链接......

<table id="shipPoints" class="ui-widget" width="697">
    <tr width="695">
    <td width="395"><a href="#" id="add-shipping-point"> Add Shipping Point </a></td>
    <td width="300">Click <img src="<%= request.getContextPath() %>/images/delete.gif"       onclick="deleteShippingPoints('shipPoints')" /> to remove checked shipping points</td>
</tr>           
</table>

调用这个 jquery 函数

$j("#add-shipping-point").click(function() {
    $j("#dialog-form").dialog("open");
    return false;
});

这是对话框的完整代码

$j(function() {
$j("#dialog:ui-dialog").dialog("destroy");

var city = $j("#city"), state = $j("#state"), zip = $j("#zip"), product = $j("#product"), allFields = $j(
        []).add(city).add(state).add(zip).add(product), tips = $j(".validateTips");

function updateTips(t) {
    tips.text(t).addClass("ui-state-highlight");
    setTimeout( function() {
        tips.removeClass("ui-state-highlight", 1500);
    }, 500);
}

function checkLength(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("Length of " + n + " must be between " + min + " and "
                + max + ".");
        return false;
    } else {
        return true;
    }
}

function checkRequired(o, n) {
    if (o.val().length == 0) {
        o.addClass("ui-state-error");
        updateTips(n + " is a required field.");
        return false;
    } else {
        return true;
    }
}

function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips("Zip Code is not in proper format.");
        return false;
    } else {
        return true;
    }
}

$j("#dialog-form")
    .dialog(
    {
    autoOpen : false,
    height : 500,
    width : 500,
    modal : true,
    buttons : {
            "Add Shipping Point" : function() {
            var bValid     = true;
            var cityValid  = true;
            var stateValid = true;
            var zipPresent = true;
            var zipValid   = true;

            updateTips("");
            allFields.removeClass("ui-state-error");

            cityValid  = checkRequired(city, "City");
            stateValid = checkRequired(state, "State");
            zipPresent = checkRequired(zip, "Zip");
            if(zipPresent) { zipValid   = checkRegexp(zip, /(^\d{5}$)|(^\d{5}-\d{4}$)/, "Zip Code"); }

            bValid     = cityValid && stateValid && zipPresent && zipValid;

    if (bValid) {
        // make Ajax call save the Shipping Point and add to the list
        var shipPointId = saveShippingPoint();

        // alert(shipPointId);

        if (shipPointId == "na") {
            alert("There was a problem adding the Shipping Point.  Please try again.  
            If the problem persists please contact support.");
        } else {
            $j("#shipPoints tr:last").after(                                                                                        
                "<tr>"
                + "<td>"
                + city.val()
                + ", "
                + state.val()
                + "</td>"
                + "<td>"
                + "<INPUT type='checkbox' NAME='chk' VALUE='"
                + shipPointId
                + "' />"
                + "</td>"
                + "</tr>");
        }
        $j(this).dialog("close");
    }
},
Cancel : function() {
$j(this).dialog("close");
    }
},
close : function() {
    allFields.val("").removeClass("ui-state-error");
}
});

$j("#add-shipping-point").click(function() {
    $j("#dialog-form").dialog("open");
    return false;
});
});

问题是您可以动态地将行添加到表中,然后如果您选中复选框并删除新创建的行,它不会删除。本质上是因为对删除运输点的调用传递了一个空白 id。

我已经阅读了它,我意识到我必须使用委托将事件绑定到新创建的角色。但是,我所拥有的语法似乎与网络上的语法不太匹配,所以我不确定我会在哪里指定委托?

有人有什么想法吗?

保存和删除代码如下

<script>
// Ajax call to add the Shipping Point to the Session
function saveShippingPoint() {

    var savedId = "";

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            savedId = xhr.responseText;
            // alert(savedId);
        }
    };      
    var url = '<portlet:resourceURL id="saveShippingPoint"/>';
    xhr.open("GET", url + 
       "?city=" + $j( "#city" ).val() +
       "&state=" + $j( "#state" ).val() +
       "&stateOther=" + $j( "#stateOther" ).val() +
       "&zip=" + $j( "#zip" ).val() +
       "&product=" + $j( "#product" ).val()
       , true);
    xhr.send();

    // alert(savedId);
    return savedId;
}

// A function to delete rows from the Shipping Points table

function deleteShippingPoints(tableID) {
var xhr = new XMLHttpRequest();

var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var shippingPointsId = "";

for ( var i = 0; i < rowCount; i++) {
    var row = table.rows[i];
    var chkbox = row.cells[1].childNodes[0];

    if (null != chkbox && true == chkbox.checked) {
        shippingPointsId = shippingPointsId + chkbox.value + ",";
        table.deleteRow(i);
        rowCount--;
        i--;
    }
}

var url = '<portlet:resourceURL id="deleteShippingPoint"/>';
xhr.open("GET", url + "?shippingPointsId=" + shippingPointsId, true);
xhr.send();
}
</script>
4

2 回答 2

1

诀窍.delegate()是您必须将其绑定到页面上已经存在的容器。保证这一点的一种方法是$(document).delegate('.myelement', 'click', myhandler);让它像.live(). 但是,如果您想获得更高的性能,请选择一个更接近您要绑定到的元素的容器:

$('#mypermanentcontainer').delegate('.mydynamicthing', 'myevent', myhandler);

更新

所以看看你的代码,你有这一行:$j(function() { ... });。我假设您将$j其用作$or的别名jQuery。如果是这样,$j(function() {});并且$j(document).ready(function(){});是完全相同的东西,即在 DOM 准备好时将运行的代码(在 jQuery 中也称为“文档准备就绪”:)。这通常是发生事件绑定的地方。你已经有了一些,你的整个$j("#dialog-form").dialog(...)部分是document.ready函数内发生的事件连接。

因此,只需将所有其余的事件接线都放在那里,就可以开始了!

于 2012-05-02T19:28:44.203 回答
1

将jquery更改为此

$j(function() {
$j("#dialog:ui-dialog").dialog("destroy");

$j("#shipPoints2").delegate(".delete-shipping-points", "click", function () {
var xhr = new XMLHttpRequest();

var table = document.getElementById('shipPoints');
var rowCount = table.rows.length;
var shippingPointsId = "";

for ( var i = 0; i < rowCount; i++) {
    var row = table.rows[i];
    var chkbox = row.cells[1].childNodes[0];

    if (null != chkbox && true == chkbox.checked) {
        shippingPointsId = shippingPointsId + chkbox.value + ",";
        table.deleteRow(i);
        rowCount--;
        i--;
    }
}

var url = '<portlet:resourceURL id="deleteShippingPoint"/>';
xhr.open("GET", url + "?shippingPointsId=" + shippingPointsId, true);
xhr.send();
} );

(其余和之前一样)

和 JSP 到这个

<div id="shipping-points-contain">
<table id="shipPoints" class="ui-widget-content" width="697">
<thead>
<tr class="ui-widget-content"  width="696">
        <th class="ui-widget-header" width="395">
    Shipping Points
    </th>
        <th class="ui-widget-header" width="300">
    Remove
    </th>
</tr>
</thead>
<tbody>
<c:forEach items="${shippingPoints}" var="shippingPoint">
<tr width="695">
    <td with="395">
        ${shippingPoint.shippingPointsCity},
        ${shippingPoint.shippingPointsState}
    </td>
    <td width="300">
    <INPUT type="checkbox" NAME="chk" value="${shippingPoint.shippingPointsId}" />
    <INPUT type="hidden" NAME="shipPointId" VALUE="${shippingPoint.shippingPointsId}" />
    </td>                           
</tr>
</c:forEach>
</tbody>
</table>
<table id="shipPoints2" class="ui-widget" width="697">
<tr width="695">
<td width="395"><a href="#" id="add-shipping-point"> Add Shipping Point </a></td>
<td width="300">Click <a href="#" id="delete-shipping-points"> <img src="<%= request.getContextPath() %>/images/delete.gif"  /></a>  remove checked shipping points</td>
</tr>           
</table>
</div>

而且至少我没有收到任何语法错误,页面显示正确并且添加仍然有效,但是当我单击图像时不会触发删除,所以事件绑定还没有......

于 2012-05-04T15:27:20.103 回答