如果用户未从下拉列表中选择有效的选项(“请选择”),我需要提醒用户“选择有效的拒绝原因”,并将其从出现的位置返回到对话框,而不提交更新。
用try-catch-throw编写的原始代码,我试图添加一个条件并抛出“请选择一个有效的原因”
试图删除try-catch-throw并添加var valid = true和条件检查
<table class="button">
<tr>
<td>
<input type="submit" onclick="Save();return false;" value="Save" class="sButton">
<input type="button" onclick="Cancel();" value="Cancel" class="sButton">
</td>
</tr>
</table>
function getReasons(ddlReason)
{
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Reason for Rejection')/Items?$orderby=Title",
type: "GET",
async: false,
headers: {"accept": "application/json;odata=verbose"},
success: function (data) {
if (data != undefined && data.d != undefined && data.d.results != undefined && data.d.results.length > 0) {
/////////////
var option = document.createElement("option");
option.text = "Please Select";
option.value = "-1";
ddlReason.appendChild(option);
$.each(data.d.results, function( index, value )
{
var option = document.createElement("option");
option.text = value.Title;
option.value = value.Id;
ddlReason.appendChild(option);
});
}
},
error: function (xhr) {
alert(xhr.status + ': ' + xhr.statusText);
}
});
}
function Save()
{
var valid = true;
SP.SOD.executeFunc("sp.js", 'SP.ClientContext', function()
{
// try
{
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
var oList = clientContext.get_web().get_lists().getByTitle('Invoice History');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('InvoiceID', window.frameElement.dialogArgs.InvoiceID);
oListItem.set_item('Title',window.frameElement.dialogArgs.InvoiceNo);
oListItem.set_item('RejectionReason',$("#ddlRejection option:selected").text());
oListItem.set_item('Description', $("#rejectionComment").val());
oListItem.update();
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
{
if(RejectionReason == "Please Select")
valid = false
}
}
if(valid == false)
{
alert("Please select a valid Reason for Rejection");
// catch(err)
}
{
// alert(err.message);
}
});
}
我认为我遇到的部分问题是创建此订单所需的顺序。